import json
from django.core import serializers
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from sjgapp.models import myali
from django.db.models import Q

@csrf_exempt
def get_by_page(request):
    info = request.body
    info = json.loads(info)
    pagenow = int(info['pagenow'])
    pagesize = int(info['pagesize'])
    start = (pagenow-1)*pagesize
    end = (pagenow-1)*pagesize+pagesize
    search = info['search']
    results = myali.objects.filter(Q(server_name__icontains=search) | Q(server_id__icontains=search))[start:end]
    results = serializers.serialize('json', results)

    counts = myali.objects.filter(Q(server_name__icontains=search) | Q(server_id__icontains=search)).count()
    return HttpResponse(json.dumps( {"result": json.loads(results), "allcount": counts} ) )